package wxshop.web.user.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by yvdedu.com on 2017/12/28.
 */

@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class FormResult {
	
	private boolean status;
	private Object data;
	private String error;
	private Map<String, String> fieldErrors;
	
	private String token;
	private Set<String> userUrls;
	
	public FormResult (Object data) {
		this.data = data;
	}
	
	public FormResult (boolean status, String token, Set<String> userUrls) {
		this.status = status;
		this.token = token;
		this.userUrls = userUrls;
	}
	
	public FormResult (String error, String field) {
		this.error = error;
		addFieldError(field, error);
	}
	
	public void addFieldError(String field, String error) {
		if (fieldErrors == null) {
			fieldErrors = new HashMap<>();
		}
		fieldErrors.put(field, error);
	}
	
	public FormResult (String error) {
		this.error = error;
	}
	
	public FormResult (boolean status, Object data) {
		this.status = status;
		this.data = data;
	}
	
	public FormResult (boolean status) {
		this.status = status;
	}
	
	public FormResult () {
	}
	
	public FormResult (List<FieldError> errors) {
		for (FieldError e : errors) {
			addFieldError(e.getField(), e.getDefaultMessage());
		}
		
	}
	
	public boolean isStatus() {
		return status;
	}
	
	public void setStatus(boolean status) {
		this.status = status;
	}
	
	public Object getData() {
		return data;
	}
	
	public void setData(Object data) {
		this.data = data;
	}
	
	public String getError() {
		return error==null&&fieldErrors!=null?fieldErrors.values().iterator().next():error;
	}
	
	public void setError(String error) {
		this.error = error;
	}
	
	public Map<String, String> getFiedErrors() {
		return fieldErrors;
	}
	
	public void setFiedErrors(Map<String, String> fiedErrors) {
		this.fieldErrors = fiedErrors;
	}
	
	public String getToken () {
		return token;
	}
	
	public void setToken (String token) {
		this.token = token;
	}
	
	public Set<String> getUserUrls () {
		return userUrls;
	}
	
	public void setUserUrls (Set<String> accesUrls) {
		this.userUrls = accesUrls;
	}
}
